package com.supms.patrol.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.supms.patrol.domain.DailyInspectionEval;
import com.supms.patrol.mapper.DailyInspectionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.supms.patrol.mapper.DailyInspectionItemsMapper;
import com.supms.patrol.domain.DailyInspectionItems;
import com.supms.patrol.service.IDailyInspectionItemsService;

/**
 * 日常巡查项Service业务层处理
 * 
 * @author LiangHao
 * @date 2022-03-14
 */
@Service
public class DailyInspectionItemsServiceImpl implements IDailyInspectionItemsService 
{
    @Autowired
    private DailyInspectionItemsMapper dailyInspectionItemsMapper;

    @Autowired
    private DailyInspectionMapper dailyInspectionMapper;

    /**
     * 查询日常巡查项
     * 
     * @param id 日常巡查项主键
     * @return 日常巡查项
     */
    @Override
    public DailyInspectionItems selectDailyInspectionItemsById(Long id)
    {
        return dailyInspectionItemsMapper.selectDailyInspectionItemsById(id);
    }

    /**
     * 查询日常巡查项列表
     * 
     * @param dailyInspectionItems 日常巡查项
     * @return 日常巡查项
     */
    @Override
    public List<DailyInspectionItems> selectDailyInspectionItemsList(DailyInspectionItems dailyInspectionItems)
    {
        return dailyInspectionItemsMapper.selectDailyInspectionItemsList(dailyInspectionItems);
    }

    /**
     * 新增日常巡查项
     * 
     * @param dailyInspectionItems 日常巡查项
     * @return 结果
     */
    @Override
    public int insertDailyInspectionItems(DailyInspectionItems dailyInspectionItems)
    {
        dailyInspectionItems.setCreateTime(DateUtils.getNowDate());
        return dailyInspectionItemsMapper.insertDailyInspectionItems(dailyInspectionItems);
    }

    /**
     * 修改日常巡查项
     * 
     * @param dailyInspectionItems 日常巡查项
     * @return 结果
     */
    @Override
    public int updateDailyInspectionItems(DailyInspectionItems dailyInspectionItems)
    {
        dailyInspectionItems.setUpdateTime(DateUtils.getNowDate());
        return dailyInspectionItemsMapper.updateDailyInspectionItems(dailyInspectionItems);
    }

    /**
     * 批量删除日常巡查项
     * 
     * @param ids 需要删除的日常巡查项主键
     * @return 结果
     */
    @Override
    public int deleteDailyInspectionItemsByIds(Long[] ids)
    {
        return dailyInspectionItemsMapper.deleteDailyInspectionItemsByIds(ids);
    }

    /**
     * 删除日常巡查项信息
     * 
     * @param id 日常巡查项主键
     * @return 结果
     */
    @Override
    public int deleteDailyInspectionItemsById(Long id)
    {
        return dailyInspectionItemsMapper.deleteDailyInspectionItemsById(id);
    }

    /**
     * 获取当前记录表的日常巡查记录
     *
     * @param dailyInspectionId 日常巡查表ID
     * @return 日常巡查记录表和记录结果
     */
    @Override
    public List<DailyInspectionItems> getDailyInspectionItemByDIId(Long dailyInspectionId) {
        return dailyInspectionItemsMapper.getDailyInspectionItemByDIId(dailyInspectionId);
    }

    /**
     * 添加日常巡查情况记录
     *
     * @param dailyInspectionItemsList 提交的日常巡查情况记录表
     * @return 结果
     */
    @Override
    public int insertDailyInspectionEval(List<DailyInspectionItems> dailyInspectionItemsList) {
        List<DailyInspectionEval> dailyInspectionEvalList = new ArrayList<>();
        Long dailyInspectionId = null;

        for (DailyInspectionItems dailyInspectionItems : dailyInspectionItemsList) {
            DailyInspectionEval dailyInspectionEval = new DailyInspectionEval();
            dailyInspectionId = dailyInspectionItems.getDailyInspectionId();

            dailyInspectionEval.setDailyInspectionId(dailyInspectionId);
            dailyInspectionEval.setDailyInspectionItemsId(dailyInspectionItems.getId());
            dailyInspectionEval.setSituation(dailyInspectionItems.getMySituation());
            dailyInspectionEval.setSuggest(dailyInspectionItems.getMySuggest());
            dailyInspectionEval.setCreateTime(DateUtils.getNowDate());

            dailyInspectionEvalList.add(dailyInspectionEval);
        }

        // 将巡查记录标记改为已评价（"1"）
        dailyInspectionMapper.updateDailyInspectionEvalFlagById(dailyInspectionId);
        return dailyInspectionItemsMapper.insertDailyInspectionEval(dailyInspectionEvalList);
    }

    /**
     * 修改（更新）日常巡查情况记录
     *
     * @param dailyInspectionItemsList 提交的日常巡查情况记录表
     * @return 结果
     */
    @Override
    public int updateDailyInspectionEval(List<DailyInspectionItems> dailyInspectionItemsList) {
        // 批量更新数据效率太低，可以采用先删除后插入的方法更高效
        List<DailyInspectionEval> dailyInspectionEvalList = new ArrayList<>();
        Long dailyInspectionId = null;

        for (DailyInspectionItems dailyInspectionItems : dailyInspectionItemsList) {
            DailyInspectionEval dailyInspectionEval = new DailyInspectionEval();
            dailyInspectionId = dailyInspectionItems.getDailyInspectionId();

            dailyInspectionEval.setDailyInspectionId(dailyInspectionId);
            dailyInspectionEval.setDailyInspectionItemsId(dailyInspectionItems.getId());
            dailyInspectionEval.setSituation(dailyInspectionItems.getMySituation());
            dailyInspectionEval.setSuggest(dailyInspectionItems.getMySuggest());
            dailyInspectionEval.setCreateTime(DateUtils.getNowDate());

            dailyInspectionEvalList.add(dailyInspectionEval);
        }

        // 先删除日常巡查情况记录表原有的数据
        dailyInspectionItemsMapper.deleteDailyInspectionEvalByDailyInspectionId(dailyInspectionId);
        return dailyInspectionItemsMapper.insertDailyInspectionEval(dailyInspectionEvalList);
    }
}
